<template>
  <div class="app-container">
    <el-row>
      <h1>当前显示的是打印日期为:{{ this.nowDate }}的数据,请选择对应的工号进行打印。</h1>

      <!-- 表头按钮组 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-printer"
            size="mini"
            :disabled="multiple"
            @click="handlePrint"
          >打印</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:user:remove']"
          >删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
      </el-row>

      <!-- 后台回显表格 -->
      <el-table v-loading="loading" :data="pellList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="产线" align="center" key="areaID" prop="areaID" v-if="columns[0].visible"  />
        <el-table-column label="产品名称" align="center" key="defName" prop="defName" width="200" v-if="columns[1].visible" :show-overflow-tooltip="true" />
        <el-table-column label="定重" align="center" key="gy_dz" prop="gy_dz" width="100" v-if="columns[2].visible" :show-overflow-tooltip="true" />
        <el-table-column label="班组" align="center" prop="team" width="50" v-if="columns[2].visible" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <p v-if="scope.row.team=='1'">甲班</p>
            <p v-if="scope.row.team=='2'">乙班</p>
            <p v-if="scope.row.team=='3'">丙班</p>
          </template>
        </el-table-column>
        <el-table-column label="工号" align="center" width="50" key="winderName" prop="winderName" v-if="columns[3].visible" :show-overflow-tooltip="true" />
        <el-table-column label="底部描述是否打印" width="80" align="center" prop="is_bottom" v-if="columns[4].visible" >
          <template slot-scope="scope">
            <p v-if="scope.row.is_bottom=='0'">不打印</p>
            <p v-if="scope.row.is_bottom=='1'">打印</p>
          </template>
        </el-table-column>
        <el-table-column label="定重是否打印" width="80" align="center"  prop="is_dz" v-if="columns[5].visible">
          <template slot-scope="scope">
            <p v-if="scope.row.is_dz=='0'">不打印</p>
            <p v-if="scope.row.is_dz=='1'">打印</p>
          </template>
        </el-table-column>
        <el-table-column label="顺逆区分" align="center" width="80" prop="is_sn" v-if="columns[6].visible">
          <template slot-scope="scope">
            <p v-if="scope.row.is_sn=='0'">不打印</p>
            <p v-if="scope.row.is_sn=='1'">顺时针</p>
            <p v-if="scope.row.is_sn=='2'">逆时针</p>
          </template>
        </el-table-column>
        <el-table-column label="日期方式" align="center" prop="is_date" v-if="columns[7].visible" width="120">
          <template slot-scope="scope">
            <p v-if="scope.row.is_date=='0'">不打印</p>
            <p v-if="scope.row.is_date=='1'">格式:年-月-日</p>
            <p v-if="scope.row.is_date=='2'">格式:日-月-年</p>
          </template>
        </el-table-column>
        <el-table-column label="打印张数" align="center" key="printcount" prop="printcount" v-if="columns[8].visible" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-input v-model="scope.row.printcount"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope" v-if="scope.row.productId !== 1">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-printer"
              @click="handlePrint(scope.row)"
            >打印</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 底部pagination -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
  </div>

</template>

<script>
import { delPutPell, updatePell } from '@/api/wind/print/print'
import { putList } from '@/api/wind/employee/employee'

export default {
  name: 'Employee',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      busPellList:[],
      defNames:[],
      // 当前日期
      nowDate: "",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //络纱标签数据
      pellList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaID: undefined,
        team:undefined,
        winderName:undefined,
        printdate:undefined
      },
      // 列信息
      columns: [
        { key: 0, label: `产线`, visible: true },
        { key: 1, label: `产品名称`, visible: true },
        { key: 2, label: `定重`, visible: true },
        { key: 2, label: `班组`, visible: true },
        { key: 3, label: `工号`, visible: true },
        { key: 4, label: `底部描述是否打印`, visible: true },
        { key: 5, label: `定重是否打印`, visible: true },
        { key: 6, label: `顺逆区分`, visible: true },
        { key: 7, label: `日期方式`, visible: true  },
        { key: 8, label: `打印张数`, visible: true  },
      ],
      areaIDOptions: [{
        "label": "304",
        "value": "0101304"
      }, {
        "label": "307",
        "value": "0101307"
      }],
      teamOptions: [{
        "label": "甲班",
        "value": "1"
      }, {
        "label": "乙班",
        "value": "2"
      }, {"label": "丙班",
        "value": "3"
      }]
    };
  },
  created() {
    this.formatDate();
    this.getList();
  },
  methods: {
    /** 查询产品列表 */
    getList() {
      this.loading = true;
      this.queryParams.printdate = this.nowDate;
      putList(this.queryParams).then(response => {
          this.pellList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 打印按钮操作 */
    handlePrint(row) {
      let putIds  = [];

      let list = [];
      if(row.guid){
        list.push(row);
        putIds.push(row.guid);
      } else {
        list = this.busPellList;
        putIds = this.ids;
      }

      let formData = new FormData();
      formData.append("list", JSON.stringify(list));

      updatePell(formData).then(response => {
        window.open("/wind/put/" + encodeURIComponent(JSON.stringify(putIds)));
      });

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.guid || this.ids;
      const defNames = row.defName || this.defNames;

      this.$confirm('是否确认删除产品名称为"' + defNames + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delPutPell(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 多选框选中数据 **/
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.guid);
      this.defNames = selection.map(item => item.defName);
      this.busPellList = selection.map(item => item);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    formatDate() {
      let date = new Date();
      let year = date.getFullYear(); // 年
      let month = date.getMonth() + 1; // 月
      let day = date.getDate(); // 日
      let week = date.getDay(); // 星期
      let weekArr = [ "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" ];
      let hour = date.getHours(); // 时
      hour = hour < 10 ? "0" + hour : hour; // 如果只有一位，则前面补零
      let minute = date.getMinutes(); // 分
      minute = minute < 10 ? "0" + minute : minute; // 如果只有一位，则前面补零
      let second = date.getSeconds(); // 秒
      second = second < 10 ? "0" + second : second; // 如果只有一位，则前面补零
      this.nowDate = `${year}-${month}-${day}`;
    }
  }
}
</script>

<style scoped>

</style>
